var util = require('../../utils/util.js')
var store = require('../../utils/store.js')
var config = require('../../utils/config.js')
var login = require('../../utils/login.js')
var API_URL = config.API_URL;
var app = getApp();
var page, loading;
/* 获取购物车列表 */
var getCart = that => {
    if (loading) {
        return
    }
    loading = true;
    util.PRL(API_URL.cart_list, { page: page })
        .then(res => {
            console.log(res)
            var data = res.data.data.list;
            var goods = that.data.goods;
            var total_price = that.data.total_price;
            var loadingText;
            if (page == 1) {
                goods = new Array();
            } else {
                goods = that.data.goods;
            }
            data = data.map(item => {
                item.checked = true;
                return item;
            })
            goods = goods.concat(data);
            if (res.data.data.hasMore == 0) {
                loadingText = '没有更多了';
            } else {
                loadingText = '加载中...'
            }
            if (data.length != 0) {
                page++
            }
            total_price = goods.reduce((a, b) => {
                return a + b.goodsPrice * b.goodsQuantity;
            }, 0)
            that.setData({
                goods: goods,
                total_price: total_price,
                loadingText: loadingText
            })
            page++;
            loading = false;
            my.stopPullDownRefresh();
        }).catch(res => {
            loading = false;
            my.stopPullDownRefresh();
            console.log(res);
        })
}
/* 算总价 */
let compute = goods => {
    return goods.reduce((a, b) => {
        if (b.checked) {
            a += b.goodsPrice * b.goodsQuantity;
        }
        return a;
    }, 0)
}
/* 判断是否全选 */
let check_all = goods => {
    return goods.reduce((a, b) => {
        if (!b.checked) {
            a = false;
        }
        return a;
    }, true)
}
Page({
    data: {
        total_price: 0,
        goods: [],
        loadingText: '',
        isAll: true
    },
    onLoad: function () {
        page = 1;
        loading = false;
        getCart(this);
    },
    choseCheck: function (e) {
        console.log('click')
        console.log(e);
        var good_index = e.currentTarget.dataset.gindex;
        var goods = this.data.goods;
        console.log(goods)
        goods[good_index].checked = !goods[good_index].checked;
        let total_price = compute(goods);
        let isAll = check_all(goods);
        this.setData({
            goods: goods,
            total_price: total_price,
            isAll: isAll
        });
    },
    // 全选
    checkAll: function (e) {
        var isAll = this.data.isAll;
        var goods = this.data.goods;
        var total_price;
        if (isAll) {
            goods = goods.map(item => {
                item.checked = false;
                return item;
            })
            total_price = 0;
        } else {
            goods = goods.map(item => {
                item.checked = true;
                return item;
            })
            total_price = compute(goods);
        }
        this.setData({
            isAll: !isAll,
            goods: goods,
            total_price: total_price
        })
    },
    // 跳转商品详情
    goToGood: function (e) {
        let id = e.currentTarget.dataset.id
        console.log(id)
        // console.log(e);
        // store.goToGood(e.currentTarget.dataset.id);
        my.navigateTo({
            url: "/pages/good/good?id=" + id,
        })
    },
    // 结算
    submit: function (e) {
        console.log(e);
        var recArr = new Array;
        recArr = e.detail.value.check;
        var recStr = recArr.join('|');
        // 拿到商品的ID
        console.log(recStr);
        /* let arr = this.data.goods.map(item => {
          let str = item.cartId + "," + item.goodsQuantity;
          return str;
        })
        let data = arr.join("|");
        util.PRL(API_URL.cart_updata, { data: data })
          .then(res => {
            wx.navigateTo({
              url: '/pages/cart_order/order?rec=' + recStr
            })
          }).catch(res => {
            console.log(res)
          }) */
        my.navigateTo({
            url: '/pages/cart-order/cart-order?rec=' + recStr
        })
    },
    // 跳转购物车
      goToMall: function (e) {
    my.switchTab({
      url: '/pages/mail/mail',
    })
  }
})